﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TN_CMS.Admin.Models;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Concrete;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Admin.Controllers
{
    public class AccountController : TNController
    {
        private ISysAdminRepository _sysAdminRepository;

        public AccountController(
            ISysAdminRepository sysAdminRepository)
        {
            _sysAdminRepository = sysAdminRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            var model = new LoginModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Login(string uid, string hashedPass)
        {
            var result = new CustomJsonResult();

            try
            {
                var sysAdmin = this._sysAdminRepository.Login(uid, hashedPass);

                if (sysAdmin != null)
                {
                    SysAdmin = new SysAdminShortInfoEntity
                    {
                        Id = sysAdmin.Id,
                        Name = sysAdmin.Name
                    };
                }
                else
                {
                    result.Message = "UserName or Password is invalid, please try again.";
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }
    }
}
